/**
 * GTActivityNotifier.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import "GTActivityNotifier.h"
#import "UIKit+GTExtensions.h"
#import "UIWindowsApplicationDelegate.h"

static CGFloat const ACTIVITY_MESSAGE_WIDTH = 320.0;
static CGFloat const ACTIVITY_MESSAGE_WIDTH_REDUCTION_FOR_SIZE_COMPUTATION = 20.0;
static CGFloat const VERTICAL_SPACE_BETWEEN_COMPONENTS = 30.0;
static CGFloat const MAX_ACTIVITY_MESSAGE_HEIGHT = 400.0;

static GTActivityNotifier *singleton;

@interface GTActivityNotifier ()
- (void)notifyStartActivity;
- (void)notifyStartActivityWithAlpha:(CGFloat)alpha;
- (void)notifyMessage:(NSString*)aMessage;
- (void)notifyMessageToConfirm:(NSString*)aMessage;
- (void)notifyEndActivity;
@property(nonatomic,retain) UIView *activityView;
@property(nonatomic,retain) UIActivityIndicatorView *activityWheel;
@property(nonatomic,retain) UITextView *activityMessage;
@property(nonatomic,retain) UIButton *okButton;  
@end

@implementation GTActivityNotifier
@synthesize activityView, activityWheel, activityMessage, okButton;

+ (void)notifyStartActivity {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivity];
}

+ (void)notifyStartActivityWithAlpha:(CGFloat)alpha {
  if (singleton) {
    [GTActivityNotifier notifyEndActivity];
  }
  singleton = [[GTActivityNotifier alloc] init];
  [singleton notifyStartActivityWithAlpha:alpha];
}

+ (void)notifyMessage:(NSString*)aMessage {
  [singleton performSelectorOnMainThread:@selector(notifyMessage:) withObject:aMessage waitUntilDone:YES];
}

+ (void)notifyMessageToConfirm:(NSString*)aMessage {
  [singleton performSelectorOnMainThread:@selector(notifyMessageToConfirm:) withObject:aMessage waitUntilDone:YES];
}

+ (void)notifyEndActivity {
  [singleton performSelectorOnMainThread:@selector(notifyEndActivity) withObject:nil waitUntilDone:YES];
  [singleton performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:YES];
  singleton = nil;
}

- (void)dealloc {
  [activityWheel stopAnimating];
  [activityWheel release];
  [activityView removeFromSuperview];
  [activityView release];
  [activityMessage release];
  [okButton release];
  [super dealloc];
}

- (void)notifyStartActivity {
  [self notifyStartActivityWithAlpha:0.5f];
}

- (void)notifyStartActivityWithAlpha:(CGFloat)alpha {
  UIWindow *window = [(id<UIWindowsApplicationDelegate>)[[UIApplication sharedApplication] delegate] window];
  
  // Activity view
	self.activityView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, window.bounds.size.width, window.bounds.size.height)] autorelease];  
	self.activityView.backgroundColor = [UIColor blackColor];
	self.activityView.alpha = alpha;
  self.activityView.userInteractionEnabled = YES;
  
  // Activity wheel
  CGRect activityWheelRect = CGRectMake(self.activityView.frame.size.width / 2 - 15, self.activityView.frame.size.height / 4 - 15, 30, 30);
  self.activityWheel = [[[UIActivityIndicatorView alloc] initFlexibleWithFrame:activityWheelRect
                                                    activityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
	[self.activityView addSubview:self.activityWheel];
  
  // Activity message
  self.activityMessage = [[[UITextView alloc] initWithFrame:CGRectMake(0.0,
                                                                       self.activityWheel.frame.origin.y + activityWheel.frame.size.height + VERTICAL_SPACE_BETWEEN_COMPONENTS,
                                                                       0.0,
                                                                       0.0)] autorelease];
  self.activityMessage.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
  self.activityMessage.textAlignment = UITextAlignmentCenter;
  self.activityMessage.backgroundColor = [UIColor clearColor];
  self.activityMessage.textColor = [UIColor whiteColor];
  self.activityMessage.editable = FALSE;
  self.activityMessage.scrollEnabled = FALSE;
  self.activityMessage.text = @"";
  [self.activityView addSubview:self.activityMessage];
  
  self.okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  self.okButton.frame = CGRectMake(0.0, 0.0, 200, 33.0);
  [self.okButton setTitle:@"OK" forState:UIControlStateNormal];
  [self.okButton addTarget:self action:@selector(notifyEndActivity) forControlEvents:UIControlEventTouchUpInside];      
  
	[window.rootViewController.view addSubview:self.activityView];
  [window.rootViewController.view bringSubviewToFront:self.activityView];  
  
	[self.activityWheel startAnimating];
}

- (void)notifyMessage:(NSString*)aMessage {
  if ([self.okButton superview]) {
    [self.okButton removeFromSuperview];
  }
  CGSize size = [aMessage sizeWithFont:self.activityMessage.font
                     constrainedToSize:CGSizeMake(ACTIVITY_MESSAGE_WIDTH - ACTIVITY_MESSAGE_WIDTH_REDUCTION_FOR_SIZE_COMPUTATION, MAX_ACTIVITY_MESSAGE_HEIGHT)
                         lineBreakMode:UILineBreakModeWordWrap];
  [self.activityMessage setFrame:CGRectMake(self.activityMessage.frame.origin.x,
                                            self.activityMessage.frame.origin.y,
                                            ACTIVITY_MESSAGE_WIDTH,
                                            size.height + 20)];
  [self.activityMessage performSelectorOnMainThread:@selector(setText:) withObject:aMessage waitUntilDone:YES];
  [self.activityMessage performSelectorOnMainThread:@selector(setTextColor:) withObject:[UIColor whiteColor] waitUntilDone:YES];
}

- (void)notifyMessageToConfirm:(NSString*)aMessage {
  [self notifyMessage:aMessage];
  self.okButton.center = CGPointMake(160.0,
                                     self.activityMessage.frame.origin.y + self.activityMessage.frame.size.height + VERTICAL_SPACE_BETWEEN_COMPONENTS);
  [self.activityView addSubview:self.okButton];
}

- (void)notifyEndActivity {
  [self.activityView removeFromSuperview];
}

@end
